const mongoose = require('mongoose');

const ChangelogSchema = new mongoose.Schema({
  personnelId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Personnel',
    required: true
  },
  sourceId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Source',
    required: true
  },
  changeType: {
    type: String,
    enum: ['new', 'updated', 'removed'],
    required: true
  },
  changedFields: [
    {
      field: String,
      oldValue: mongoose.Schema.Types.Mixed,
      newValue: mongoose.Schema.Types.Mixed
    }
  ],
  timestamp: {
    type: Date,
    default: Date.now
  },
  details: {
    type: String,
    required: false
  }
}, { timestamps: true });

// 索引，用于提高查询性能
ChangelogSchema.index({ personnelId: 1 });
ChangelogSchema.index({ sourceId: 1 });
ChangelogSchema.index({ timestamp: -1 });
ChangelogSchema.index({ changeType: 1 });

module.exports = mongoose.model('Changelog', ChangelogSchema); 